package de.tabuma.sso.groupprivileg;

import java.util.*;
import java.util.logging.*;

import de.tabuma.sso.*;
import de.tabuma.sso.group.*;
import de.tabuma.sso.privileg.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.user.*;
import de.tabuma.shopping.logik.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.2.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class GroupPrivilegWebservice {

    private static Logger log = Logger.getLogger(GroupPrivilegWebservice.class.getName());

    public static int create(GroupPrivileg groupPrivileg, String ssoId) throws Exception {
        int groupId = GroupPrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUPPRIVILEG_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        
        GroupPrivilegWebserviceLib.userHasPrivilegForGroupPrivileg(groupPrivileg, ssoId);
        return GroupPrivilegFacade.create(groupPrivileg, user.getName());
    }

    public static void update(GroupPrivileg groupPrivileg, String ssoId) throws Exception {
        int groupId = GroupPrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUPPRIVILEG_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        GroupPrivilegWebserviceLib.userHasPrivilegForGroupPrivileg(groupPrivileg, ssoId);
        GroupPrivilegFacade.update(groupPrivileg, user.getName());
    }

    public static GroupPrivileg get(int id, String ssoId) throws Exception {
        int groupId = GroupPrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUPPRIVILEG_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        
        GroupPrivileg groupPrivileg = GroupPrivilegFacade.get(id);
        GroupPrivilegWebserviceLib.userHasPrivilegForGroupPrivileg(groupPrivileg, ssoId);
        return groupPrivileg;
    }

    public static List<GroupPrivileg> getList(int groupId1, String ssoId) throws Exception {
        int groupId = GroupPrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUPPRIVILEG_GETLIST1, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        GroupWebserviceLib.userHasPrivilegForGroup(GroupFacade.get(groupId1), ssoId);
        List<GroupPrivileg> gpList = GroupPrivilegFacade.getList(groupId1);
        return gpList;
    }

    public static List<GroupPrivileg> getList1(int privilegId, String ssoId) throws Exception {
        int groupId = GroupPrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUPPRIVILEG_GETLIST2, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        PrivilegWebserviceLib.userHasPrivilegForPrivileg(PrivilegFacade.get(privilegId), ssoId);
        return GroupPrivilegFacade.getList1(privilegId);
    }

    public static void remove(int groupPrivilegId, String ssoId) throws Exception {
        int groupId = GroupPrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUPPRIVILEG_GETLIST2, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        GroupPrivilegWebserviceLib.userHasPrivilegForGroupPrivileg(GroupPrivilegFacade.get(groupPrivilegId), ssoId);
        GroupPrivilegFacade.remove(groupPrivilegId, user.getName());
    }
}